package movee.api.v1;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import movee.domain.api.CountedSuccessResponse;
import movee.domain.api.SuccessResponse;
import movee.domain.dto.BackupConfigDto;
import movee.domain.query.AppConfigSaveQuery;
import movee.domain.query.AppConfigSearchQuery;
import movee.service.AppConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import jakarta.validation.Valid;
import java.util.List;

/**
 * restful controller层
 *
 * @author movee
 */
@RestController
@RequestMapping(value = "/v1/app/config", produces = "application/json")
@Slf4j
@Tag(name = "配置管理")
public class ApiAppConfigControllerV1 {

    private final AppConfigService configService;

    @Autowired
    public ApiAppConfigControllerV1(AppConfigService configService) {
        this.configService = configService;
    }

    /**
     * 查询
     * @param query 查询条件
     * @return 符合条件的表项
     */
    @Operation(summary = "查询列表")
    @PostMapping(value = "/list")
    public CountedSuccessResponse<List<BackupConfigDto>> getAppConfigList(
            @Parameter(name = "查询条件") @Valid @RequestBody AppConfigSearchQuery query) {

        return new CountedSuccessResponse<>(configService.getAppConfigList(query));
    }

    /**
     * 创建或编辑配置
     * @param query 查询条件
     * @return 保存后的任务
     */
    @Operation(summary = "创建或编辑配置")
    @PostMapping(value = "/action/save")
    public SuccessResponse<List<BackupConfigDto>> saveAppConfig(
            @Parameter(name = "查询条件") @Valid @RequestBody AppConfigSaveQuery query) {

        return new SuccessResponse<>(configService.saveAppConfig(query));
    }

    /**
     * 删除配置
     * @param ids 删除的id列表
     * @return success or fail
     */
    @Operation(summary = "删除配置")
    @DeleteMapping(value = "/action/delete")
    public SuccessResponse<String> deleteAppConfig(
            @Parameter(name = "删除的id列表") @RequestParam List<Long> ids) {

        configService.deleteAppConfig(ids);
        return new SuccessResponse<>("success");
    }
}
